package com.sky.merchant.controller;

import com.sky.common.annotation.Anonymous;
import com.sky.common.core.domain.AjaxResult;
import com.sky.merchant.domain.ResShop;
import com.sky.merchant.service.ResShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Anonymous
@RestController
@RequestMapping("/shops")
public class ResShopController {

    @Autowired
    private ResShopService resShopService;

//    {
//        "name": "新店铺名称",
//            "address": "新店铺地址",
//            "contact": "1234567890",
//            "cover": "http://example.com/cover.jpg",
//            "detail": "这是新店铺的详细描述。",
//            "star": 4,
//            "ownerId": 12345,
//            "createBy": "admin",
//            "createTime": "2023-04-01T12:00:00",
//            "updateBy": "admin",
//            "updateTime": "2023-04-01T12:00:00"
//    }
    @PostMapping
    public AjaxResult addShop( @RequestBody ResShop resShop) {
        int result = resShopService.addShop(resShop);
        if (result > 0) {
            return AjaxResult.success(null); // 或者返回新添加的店铺信息
        } else {
            return AjaxResult.error("添加店铺失败");
        }
    }

    @DeleteMapping("/{id}")
    public AjaxResult removeShopById(@PathVariable Long id) {
        int result = resShopService.removeShopById(id);
        if (result > 0) {
            return AjaxResult.success(null);
        } else {
            return AjaxResult.error("删除店铺失败");
        }
    }

    @PutMapping
    public AjaxResult updateResShop(@RequestBody ResShop resShop) {
        int result = resShopService.updateResShop(resShop);
        if (result > 0) {
            return AjaxResult.success(null); // 或者返回更新后的店铺信息
        } else {
            return AjaxResult.error("更新店铺失败");
        }
    }

    @GetMapping("/{id}")
    public AjaxResult getShopById(@PathVariable Long id) {
        ResShop resShop = resShopService.getShopById(id);
        if (resShop != null) {
            return AjaxResult.success(resShop);
        } else {
            return AjaxResult.error("店铺不存在");
        }
    }

    @GetMapping
    public AjaxResult getAllShops() {
        List<ResShop> shops = resShopService.getAllShops();
        return AjaxResult.success(shops);
    }

    @PutMapping("/updateShop")
    public AjaxResult updateShop(@RequestBody ResShop resShop) {
        try {
            resShopService.updateShop(resShop);
            return AjaxResult.success(null);
        } catch (Exception e) {
            return AjaxResult.error("更新店铺时发生错误: " + e.getMessage());
        }
    }

    @PostMapping("/selectShops")
    public AjaxResult selectShops(@RequestBody Map<String, Object> params) {
        List<ResShop> shops = resShopService.selectShops(params);
        return AjaxResult.success(shops);
    }

    @PostMapping("/insertShops")
    public AjaxResult insertShops(@RequestBody List<ResShop> shops) {
        try {
            resShopService.insertShops(shops);
            return AjaxResult.success(null);
        } catch (Exception e) {
            return AjaxResult.error("插入店铺时发生错误: " + e.getMessage());
        }
    }

    @GetMapping("/searchByName")
    public AjaxResult searchResShopByName(@RequestParam String name) {
        List<ResShop> resShopList = resShopService.selectResShopByName(name);
        return AjaxResult.success(resShopList);
    }
}